# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import csv

class BaidutiebaPipeline:
    def open_spider(self, spider):
        if spider.name == 'tieba':
            self.f = open('./tieba_data.csv', 'w', newline='', encoding='utf-8')
            self.write = csv.writer(self.f, dialect=('excel'))
            self.write.writerow(['标题', '作者', '回复数'])

    def close_spider(self, spider):
        if spider.name == 'tieba':
            self.f.close()

    def process_item(self, item, spider):
        if spider.name == 'tieba':
            self.write.writerow([item['title'], item['author'], item['reply']])
        return item
